package com.gxa.service.impl.sale;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxa.common.LayR;
import com.gxa.common.R;
import com.gxa.mapper.SaleMapper;
import com.gxa.param.TableParam;
import com.gxa.pojo.Empl;
import com.gxa.pojo.sale.SaleMan;
import com.gxa.pojo.sale.SalePlan;
import com.gxa.service.SaleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.List;

@Service
public class SaleServiceImpl implements SaleService {
    @Autowired
    private SaleMapper saleMapper;
    @Override
    public LayR list(TableParam param) {

        PageHelper.startPage(param.getPageNum(), param.getPageSize());

        List<SalePlan> salePlans = saleMapper.findAll(param);
        // 封装pageInfo
        PageInfo<SalePlan> pageInfo = new PageInfo<>(salePlans);

        return new LayR(0, "success", pageInfo.getTotal(), pageInfo.getList());
    }

    @Override
    public R deleteAll(String[] ids) {

        saleMapper.deleteAll(ids);
        return new R(200, "删除成功！");
    }

    @Override
    public R detail(Integer id) {
        SalePlan salePlan = saleMapper.findByPk(id);
        return new R(200, "success", salePlan);
    }


    @Override
    public R add(SalePlan salePlan) {

        salePlan.setPlanMonth(new Timestamp(System.currentTimeMillis()));
        saleMapper.save(salePlan);
        return new R(200, "添加成功！");

    }

    @Override
    public R deleteOne(Integer id) {

        saleMapper.deleteOne(id);

        return new R(200, "删除成功！");
    }

    @Override
    public R edit(SalePlan salePlan) {

        salePlan.setPlanLatetime(new Timestamp(System.currentTimeMillis()));
        saleMapper.edit(salePlan);
        return new R(200, "添加成功！");

    }

    @Override
    public LayR contactList(TableParam param) {
        PageHelper.startPage(param.getPageNum(), param.getPageSize());

        List<SaleMan> salePlans = saleMapper.findAllcontact(param);
        // 封装pageInfo
        PageInfo<SaleMan> pageInfo = new PageInfo<>(salePlans);

        return new LayR(0, "success", pageInfo.getTotal(), pageInfo.getList());
    }

    @Override
    public R addContact(SaleMan saleMan) {
        saleMapper.addContact(saleMan);
        return new R(200, "添加成功！");
    }

    @Override
    public R ContactDeleteAll(String[] ids) {
        saleMapper.contactDeleteAll(ids);
        return new R(200, "删除成功！");
    }

    @Override
    public R contactDetail(Integer id) {
        SaleMan saleMan = saleMapper.findContactByPk(id);
        return new R(200, "success", saleMan);
    }

    @Override
    public R contactUpdate(SaleMan saleMan) {
        saleMapper.editContact(saleMan);
        return new R(200, "修改成功！");
    }


}
